
<template>
  <div>
    <BasicTable @register="registerTable" @fetchSuccess="fetchSuccess">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button @click="expandAll" :title="t('展开一级')">
          <Icon icon="bi:chevron-double-down" /> {{ t('展开') }}
        </a-button>
        <a-button @click="collapseAll" :title="t('展开全部')">
          <Icon icon="bi:chevron-double-up" /> {{ t('折叠') }}
        </a-button>
        <a-button type="primary" @click="handleForm({})" v-auth="'test:testTree:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <span class="cursor-pointer" @click="expandCollapse(record)">
          ( {{ record.operCode }} )
        </span>
        <a @click="handleForm({ operCode: record.operCode })">
          {{ record.operName }}
        </a>
       
      </template>
      <template #checkBox="{ record, column }">
       
        <a-checkbox :checked="record[column.dataIndex] == '1' ? true : false" />
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBasOperList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, watch, nextTick } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { operDelete, operList } from '/@/api/test/testTree';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';

  const props = defineProps({
    treeCode: String,
  });

  const { t } = useI18n('bas.oper');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('数据管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('工序编号'),
        field: 'operCode',
        component: 'Input',
      },
     
      {
        label: t('工序名称'),
        field: 'operName',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('工序名称'),
      dataIndex: 'operName',
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('是否委外'),
      dataIndex: 'bout',
      width: 90,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('是否检验'),
      dataIndex: 'bcheck',
      width: 90,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('是否数铣'),
      dataIndex: 'bsx',
      width: 90,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('是否入库'),
      dataIndex: 'brk',
      width: 90,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('允许补料'),
      dataIndex: 'canbl',
      width: 90,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('允许完工'),
      dataIndex: 'canOver',
      width: 90,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('合格率统计'),
      dataIndex: 'brate',
      width: 90,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('上传附件'),
      dataIndex: 'bfile',
      width: 90,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('上传图片'),
      dataIndex: 'bpic',
      width: 90,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('工序时长(正常)'),
      dataIndex: 'durtimeNor',
      width: 150,
      align: 'center',
    },
    {
      title: t('工序时长(加急)'),
      dataIndex: 'durtimeUrgent',
      width: 150,
      align: 'center',
    },
    {
      title: t('工序时长(特急)'),
      dataIndex: 'durtimeExurgent',
      width: 150,
      align: 'center',
    },
    {
      title: t('工序负责人'),
      dataIndex: 'manNames',
      width: 200,
      align: 'center',
    },
    {
      title: t('顺序号'),
      dataIndex: 'treeSort',
      width: 100,
      align: 'center',
    },
    
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑数据'),
        onClick: handleForm.bind(this, { operCode: record.operCode }),
        auth: 'bas:oper:edit',
      },
    
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除数据'),
        popConfirm: {
          title: t('是否确认删除数据'),
          confirm: handleDelete.bind(this, { operCode: record.operCode }),
        },
        auth: 'bas:oper:edit',
      },
      {
        icon: 'fluent:add-circle-24-regular',
        title: t('新建下级数据'),
        onClick: handleForm.bind(this, {
          parentCode: record.operCode,
          parentName: record.operName,
        }),
        auth: 'bas:oper:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, expandAll, collapseAll, expandCollapse }] = useTable({
    api: operList,
    beforeFetch: (params) => {
      params.id = props.treeCode;
      return params;
    },
    afterFetch: (res) => {
      return res;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    isTreeTable: true,
    pagination: false,
    canResize: true,
  });

  watch(
    () => props.treeCode,
    () => {
      reload();
    },
  );

  function fetchSuccess() {
    if (props.treeCode) {
      nextTick(expandAll);
    }
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await operDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
